package com.twsjw.dao;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.twsjw.entity.Userinfo;
import com.twsjw.util.HibernateSessionFactory;

public class userDAOImpl implements userDAO
{

	public Userinfo selectByNamePwd(String username, String userpass) {
		Userinfo u=null;
		Session session=HibernateSessionFactory.getSession();
		String hql="from Userinfo where username='"+username+"' and userpass='"+userpass+"'";
		List list=session.createQuery(hql).list();
		if(list.size()>0)
		{
			u=(Userinfo)list.get(0);
		}
		return u;
	}
	public void insert(String username, String userpass) 
	{
		Session session=HibernateSessionFactory.getSession();
		Transaction tran=session.beginTransaction();
		Userinfo u=new Userinfo(username,userpass);
		session.save(u);
		tran.commit();
		session.close();
	}
	public String getInfo(String username) 
	{
		String user_id="";
		Session session=HibernateSessionFactory.getSession();
		String hql="select user_id from Userinfo where username='"+username+"'";
		List list=session.createQuery(hql).list();
		Iterator i=list.iterator();
		if(i.hasNext())
		{
			user_id=i.next().toString();
		}
		else
		{
			user_id="";
		}
		return user_id;
	}


}
